﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameServer
{
    public partial class FrmMain : Form
    {
        public class Team
        {
            public String filename;
            public String teamname;
            public Team(String filename, String teamname)
            {
                this.filename = filename;
                this.teamname = teamname;
            }

            override public String ToString()
            {
                return teamname;
            }
        }
        public Dictionary<String, String> AIs = new Dictionary<string, string>();
        private List<ListBox> listboxs = new List<ListBox>();
        private List<String> AI_filenames;
        public String server_type = "Battle";
        public String replay_path = "";
        public String battle_file_path = "";
        public FrmMain()
        {
            InitializeComponent();
            dialog_replay_file.InitialDirectory = Environment.CurrentDirectory + @"\replay";
        }
        public FrmMain(ref List<String> AI_filenames)
            : this()
        {
            this.AI_filenames = AI_filenames;
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            listboxs.Add(listBox1);
            listboxs.Add(listBox2);
            listboxs.Add(listBox3);
            listboxs.Add(listBox4);
            refresh_AIs();
        }
        public void refresh_AIs()
        {
            var files = System.IO.Directory.EnumerateFiles(Environment.CurrentDirectory + @"\AIs\");
            AIs = new Dictionary<string, string>();
            foreach (String file in files)
            {
                String team_name = "";
                try
                {
                    AI_Container aic = new AI_Container(1);
                    aic.LoadDll(file);
                    team_name = aic.team_name;
                }
                catch (Exception)
                {
                    continue;
                }
                AIs.Add(System.IO.Path.GetFileName(file), team_name);
            }
            foreach (var l in listboxs)
                l.Items.Clear();
            AIs.OrderBy((kv) => kv.Key);
            foreach (var kv in AIs)
            {
                Team t = new Team(kv.Key, kv.Value);
                foreach (var l in listboxs)
                    l.Items.Add(t);
            }
            if (AIs.Count > 0)
            {
                foreach (var l in listboxs)
                    l.SelectedIndex = 0;
            }

        }


        private void btnRefresh_Click(object sender, EventArgs e)
        {
            refresh_AIs();
        }
        private void btnNextBattle_Click(object sender, EventArgs e)
        {
            foreach (var l in listboxs)
            {
                if (l.Items.Count > 0)
                {
                    if (l.SelectedIndex + 1 < l.Items.Count)
                        l.SelectedIndex += 1;
                    else
                        l.SelectedIndex = 0;
                }
            }
        }
        private void btnSwapPosition_Click(object sender, EventArgs e)
        {
            List<int> ss = new List<int>();
            for (int i = 0; i < 4; i++)
                ss.Add(listboxs[i].SelectedIndex);
            listboxs[0].SelectedIndex = ss[2];
            listboxs[1].SelectedIndex = ss[3];
            listboxs[2].SelectedIndex = ss[1];
            listboxs[3].SelectedIndex = ss[0];

        }
        private void setAIs()
        {

            lock (AI_filenames)
            {
                AI_filenames.Clear();
                foreach (var l in listboxs)
                {
                    if (l.SelectedIndex != -1)
                        AI_filenames.Add(((Team)l.SelectedItem).filename);
                }
            }

        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label1.Text = ((Team)listBox1.SelectedItem).filename;
            setAIs();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            label2.Text = ((Team)listBox2.SelectedItem).filename;
            setAIs();
        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            label3.Text = ((Team)listBox3.SelectedItem).filename;
            setAIs();
        }

        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            label4.Text = ((Team)listBox4.SelectedItem).filename;
            setAIs();
        }

        private void btn_select_reply_file_Click(object sender, EventArgs e)
        {
            dialog_replay_file.ShowDialog();
            replay_path = txt_replay_filename.Text = dialog_replay_file.FileName;
        }
        private void btn_select_battle_file_Click(object sender, EventArgs e)
        {
            dialog_battle_file.ShowDialog();
            battle_file_path = txt_battle_file.Text = dialog_battle_file.FileName;
        }
        private void dialog_replay_file_FileOk(object sender, CancelEventArgs e)
        {
            if (radio_replay.Checked)
                server_type = "Replay";
        }

        private void radio_battle_CheckedChanged(object sender, EventArgs e)
        {
            if (radio_battle.Checked)
                server_type = "Battle";
        }

        private void radio_battle_file_CheckedChanged(object sender, EventArgs e)
        {
            if (radio_battle_file.Checked)
                server_type = "Battle_by_file";
        }

       




    }
}
